package com.tianyu.inspection.common;

import com.alibaba.fastjson2.JSONArray;
import com.tianyu.inspection.entity.Pipeline;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class DistanceUtil {
	  
    //地球平均半径  
    private static final double EARTH_RADIUS = 6378137;  
    //把经纬度转为度（°）  
    private static double rad(double d){  
       return d * Math.PI / 180.0;  
    }  
      
    /**  
     * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米  
     * @param lng1  
     * @param lat1  
     * @param lng2  
     * @param lat2  
     * @return  
     */  
    public static double getDistance(double lng1, double lat1, double lng2, double lat2){  
       double radLat1 = rad(lat1);  
       double radLat2 = rad(lat2);  
       double a = radLat1 - radLat2;  
       double b = rad(lng1) - rad(lng2);  
       double s = 2 * Math.asin(  
            Math.sqrt(  
                Math.pow(Math.sin(a/2),2)   
                + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)  
            )  
        );  
       s = s * EARTH_RADIUS;  
       s = Math.round(s * 10000) / 10000;  
       return s;  
    }

    /**
     * 获取2点间距离
     * @param point1 [lat,lng]
     * @param point2 [lat,lng]
     * @return
     */
    public static double getDistance(double[] point1,double[] point2){
        GlobalCoordinates source = new GlobalCoordinates(point1[0], point1[1]);
        GlobalCoordinates target = new GlobalCoordinates(point2[0], point2[1]);
        return new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, source, target).getEllipsoidalDistance();
    }


      
    /**  
     * test  
     * @param args  
     */  
    public static void main(String[] args){
        List<Pipeline> pipelineList = new ArrayList<>();
        pipelineList.add(new Pipeline().setCenterPoint("[119.33385917,32.10650456]"));
        pipelineList.add(new Pipeline().setCenterPoint("[115.1413533611,27.5872432048]"));
        pipelineList.add(new Pipeline().setCenterPoint("[124.7797384758,46.3774224042]"));
        pipelineList.add(new Pipeline().setCenterPoint("[114.8529927319,26.1262262587]"));
        final Optional<Pipeline> min = pipelineList.stream().filter(pipeline -> Objects.nonNull(pipeline.getCenterPoint())).min((pipeline1, pipeline2) -> {
            final List<Double> lngLat1 = JSONArray.parseArray(pipeline1.getCenterPoint(), Double.class);
            final List<Double> lngLat2 = JSONArray.parseArray(pipeline2.getCenterPoint(), Double.class);
            final double distance1 = getDistance(new double[]{lngLat1.get(1), lngLat1.get(0)},
                    new double[]{27.5972432048, 116.1413533611});
            final double distance2 = getDistance(new double[]{lngLat2.get(1), lngLat2.get(0)},
                    new double[]{27.5972432048, 116.1413533611});
            return Double.compare(distance1, distance2);
        });
        System.out.println(min.get());
    }
  
} 